library(dplyr)
library(ggplot2)

pitch_data <- read.csv("pitch data.csv", header = TRUE)

# remove NAs
pitch_data <- pitch_data %>%
  filter(!is.na(f0))

# get means and confidence intervals
pitch_data %>%
  group_by(accent,syll,time.point) %>%
  summarise(mean_f0 = mean(f0),
            se = sd(f0) / sqrt(n()),
            count = n(),
            ci_lower = mean_f0 - qt(0.975, df = count - 1) * se,
            ci_upper = mean_f0 + qt(0.975, df = count - 1) * se) -> sum_pitch

# creates the plot
sum_pitch %>%
  ggplot(aes(x = factor(time.point,
                        levels = c("f0_1", "f0_2", "f0_3", "f0_4", "f0_5", "f0_6", "f0_7", "f0_8", "f0_9", "f0_10")),
             y = mean_f0,
             color = factor(accent,
                            levels = c("short falling", "long falling", "long rising"),
                            labels = c("short falling", "long falling", "rising")), 
             fill = factor(accent,
                           levels = c("short falling", "long falling", "long rising"),
                           labels = c("short falling", "long falling", "rising")),
             group = interaction(accent, syll))) +
  geom_line() +
  geom_ribbon(aes(ymin = ci_lower, ymax = ci_upper), alpha = 0.2, color = NA) +
  facet_wrap(~factor(syll,
                     levels = c("stressed", "post-tonic"))) +
  labs(x = "Time (normalized)", y = "Mean F0 (Hz)", color = "Accent", fill = "Accent") +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "bottom") -> pitch_plot

ggsave("pitch plot.png", pitch_plot, dpi = 600)

# get means and confidence intervals
pitch_data %>%
  group_by(accent,syll,time.point,size) %>%
  summarise(mean_f0 = mean(f0),
            se = sd(f0) / sqrt(n()),
            count = n(),
            ci_lower = mean_f0 - qt(0.975, df = count - 1) * se,
            ci_upper = mean_f0 + qt(0.975, df = count - 1) * se) -> sum_pitch_size

# creates the plot
sum_pitch_size %>%
  ggplot(aes(x = factor(time.point,
                        levels = c("f0_1", "f0_2", "f0_3", "f0_4", "f0_5", "f0_6", "f0_7", "f0_8", "f0_9", "f0_10")),
             y = mean_f0,
             color = factor(accent,
                            levels = c("short falling", "long falling", "long rising"),
                            labels = c("short falling", "long falling", "rising")), 
             fill = factor(accent,
                           levels = c("short falling", "long falling", "long rising"),
                           labels = c("short falling", "long falling", "rising")),
             group = interaction(accent, syll))) +
  geom_line() +
  geom_ribbon(aes(ymin = ci_lower, ymax = ci_upper), alpha = 0.2, color = NA) +
  facet_grid(size~factor(syll,
                     levels = c("stressed", "post-tonic"))) +
  labs(x = "Time (normalized)", y = "Mean F0 (Hz)", color = "Accent", fill = "Accent") +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "bottom",
        panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5)) -> pitch_plot_size

ggsave("pitch plot by size.png", pitch_plot_size, dpi = 600)
